package com.xzit.rental.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 实现mybatisPlus自动填充功能
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 插入时完成自动填充
        this.strictInsertFill(metaObject, "createTime",() -> LocalDateTime.now(),LocalDateTime.class);
        this.strictInsertFill(metaObject, "updateTime",() -> LocalDateTime.now(),LocalDateTime.class);
        //this.strictInsertFill(metaObject, "createTime",() -> new Date(),Date.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 更新时完成自动填充
        this.strictUpdateFill(metaObject, "updateTime",() -> LocalDateTime.now(),LocalDateTime.class);
        //this.strictUpdateFill(metaObject, "updateTime",() -> new Date(),Date.class);
    }
}
